package com.dlc.longweisheng.chongdianxian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.longweisheng.chongdianxian.R;
import com.dlc.longweisheng.chongdianxian.pub.base.BaseActivity;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.dlcqrcodemodule.QrCodeFragment;

public class ScanActivity extends BaseActivity implements QrCodeFragment.QrCodeListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private QrCodeFragment mQrCodeFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_scan;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建并添加二维码Fragment
        mQrCodeFragment = QrCodeFragment.addToParent(fragmentManager, fragmentTransaction, R.id.qr_code_container, true);
        // 提交Fragment事务
        fragmentTransaction.commit();
    }

    @OnClick(R.id.tv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 选择图片后返回
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == QrCodeFragment.REQUEST_GET_IMAGE) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null && images.size() > 0) {
                    // 解析图片二维码
                    mQrCodeFragment.parseImage(images.get(0).path);
                }
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public boolean onScanQRCodeSuccess(String result, QrCodeFragment fragment) {
        // 弹对话框界面啥的搞事情
//        fragment.startSpot(); // 再扫描
        // 这里返回false表示不自动再此扫描，可以手动调用fragment.startSpot()再次扫描
        // 返回true就表示会连续扫描了
        Intent intent = new Intent();
        intent.putExtra("content", result);
        setResult(RESULT_OK, intent);
        finish();
        return false;
    }

    @Override
    public void openCameraError() {
        showOne("打开相机失败");
    }

    @Override
    public void onClickSelectImage() {
        // 选择图片
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setMultiMode(false);
        Intent intent = new Intent(this, ImageGridActivity.class);
        startActivityForResult(intent, QrCodeFragment.REQUEST_GET_IMAGE);
    }

    @Override
    public void onParseQRCodeFailure() {
        showOne("解析二维码失败");
    }
}
